package com.robusta.spreadsheet.describe.jaxb.validation;

import com.robusta.spreadsheet.describe.Validatable;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
public class ContentValidator extends BaseValidator{
    @XmlAttribute(required = true)
    private Type type;
    private RegexValidator validator;

    public ContentValidator() {
        validator = new RegexValidator();
    }

    public ContentValidator(Type type) {
        this();
        this.type = type;
    }

    @Override
    public void validate(Validatable validatable) throws ValidationException {
        validator.pattern = type.pattern;
        try {
            validator.validate(validatable);
        } catch (ValidationException e) {
            throw new ValidationException(String.format("Input is required to be: %s", type.desc), validatable);
        }
    }

    @XmlType
    @XmlEnum
    public static enum Type {
        ALPHA("^[a-zA-Z]*$", "Aplhabetic"), ALPHANUMERIC("^[a-zA-Z0-9]*$", "Alphanumeric");
        private String pattern;
        private String desc;

        Type(String pattern, String desc) {
            this.pattern = pattern;
            this.desc = desc;
        }
    }
}
